package com.tfswx.ruleengine.compute;

import com.alibaba.fastjson.JSON;
import com.tfswx.ruleengine.compute.bean.*;
import com.tfswx.ruleengine.compute.gzjy.FelService;
import com.tfswx.ruleengine.compute.gzjy.RuleEngineUtil;
import com.tfswx.ruleengine.compute.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/8
 */
public class RuleEngine {
    private RuleEngine() {
    }

    public static Gzjyjg excute(List<FormDataObject> formDataObjectList, List<RuleInfo> ruleInfoList) {
        String hcjgbh = CommonUtil.getUuid();

        List<String> zdjh = RuleEngineUtil.getZdjh(ruleInfoList);
        List<DataObject> dataObjectList = RuleEngineUtil.filterData(formDataObjectList, zdjh, hcjgbh);
        return excute(dataObjectList, ruleInfoList, hcjgbh);
    }

    public static Gzjyjg excute(List<DataObject> dataObjectList, List<RuleInfo> ruleInfoList, String hcjgbh) {

        long startTime = System.currentTimeMillis();

        List<GzjyLog> gzjyLogs = new ArrayList<>();
        List<GzjyjgItem> gzjyjgItemList = new ArrayList<>();
        for (RuleInfo ruleInfo : ruleInfoList) {
            FelService felService = new FelService(ruleInfo, dataObjectList, hcjgbh);
            felService.exec();

            gzjyLogs.add(felService.getGzjyLog());
            gzjyjgItemList.addAll(felService.getHcjgxqList());
        }
        long hchs = System.currentTimeMillis() - startTime;
        Gzjyjg gzJyJg = new Gzjyjg();
        gzJyJg.setHcjgbh(hcjgbh);
        gzJyJg.setHchs(hchs);
        gzJyJg.setGzjyjgItemList(gzjyjgItemList);
        gzJyJg.setGzjyLogList(gzjyLogs);
        List<FormDataObject> formDataObjects = JSON.parseArray(JSON.toJSONString(dataObjectList), FormDataObject.class);
        gzJyJg.setData(formDataObjects);
        return gzJyJg;
    }
}
